package model.admin;

import java.io.File;
import java.sql.ResultSet;
import java.sql.Time;
import java.util.ArrayList;
import vo.TimeTableVo;
import javax.servlet.http.HttpSession;

import myutil.Connect;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import vo.BatchDetailsVo;

public class TimeTableMo {

	public void uploadTimeTable(int batch_id,String subject,File attached_file, String file_name,String type){
		ResultSet rs;
		String query="";
		try{
		HttpSession session = ServletActionContext.getRequest().getSession();
		String uploader = session.getAttribute("username").toString();
		if (attached_file != null) 
		{
			Connect c = new Connect();
			// int uploader_id=0;
			
			query="insert into time_table(batch_id,uploader,subject,document_name) values("+batch_id +",'"+uploader+"','"+subject+"','"+file_name+"')";
			System.out.println(query);
			c.dml(query);
			c.logger(type, "Insert");
			// System.out.println(getServlet().getServletContext().getRealPath(""));
			File f = new File(
					"F:/Metalink/attachments/batch_"+ batch_id);
			File f1=new File(
					"F:/Metalink/attachments/batch_"+ batch_id+"/TimeTable");
			
			if(f.exists()){
				
				if(f1.exists()){
					
				}else{
					f1.mkdir();
				}
			}
			else{
				f.mkdir();
				f1.mkdir();
			}
		
			File f2 = new File("F:/Metalink/attachments/batch_"+ batch_id + "/TimeTable/" +file_name);
			FileUtils.copyFile(attached_file, f2);
		
		}
	}catch (Exception e)
	{
		// TODO Auto-generated catch block
		e.printStackTrace();
	} 
		
}

	
	public ArrayList<BatchDetailsVo> getBatchList(){
		ArrayList<BatchDetailsVo> batch = new ArrayList<BatchDetailsVo>();
		String query="";
		Connect c= new Connect();
		query="select * from batch_details where status='Active'";
		System.out.println(query);
		ResultSet rs;
		try {
			rs=c.getResult(query);
			while(rs.next()){
				BatchDetailsVo vo = new BatchDetailsVo();
				vo.setBatch_id(rs.getInt("batch_id"));
				vo.setBatch_name(rs.getString("batch_name"));
				batch.add(vo);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return batch;
	}
	
	public ArrayList<TimeTableVo> getTimeTableList(){
		ArrayList<TimeTableVo> mo = new ArrayList<TimeTableVo>();
		String query="";
		Connect c = new Connect();
		ResultSet rs;
		query="select * from time_table";
		System.out.println(query);
		try {
			rs=c.getResult(query);
			while(rs.next()){
			TimeTableVo vo = new TimeTableVo();
			vo.setBatch_id(rs.getInt("batch_id"));
			vo.setSubject(rs.getString("subject"));
			vo.setUploader(rs.getString("uploader"));
			vo.setUpload_time(rs.getString("upload_time"));
			vo.setDocument_name(rs.getString("document_name"));
			vo.setDocument_id(rs.getInt("time_table_id"));
			vo.setAttachment_id(rs.getInt("time_table_id"));
			mo.add(vo);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return mo;
	}
	
	public void deleteTimeTable(int time_table_id){
		String query="",query2="",file_name="";
		int batch_id=-1;
		Connect c= new Connect();
		query="select batch_id,document_name from time_table where time_table_id="+time_table_id;
		query2="delete from time_table where time_table_id="+time_table_id;
		System.out.println(query);
		System.out.println(query2);
		try {
			ResultSet rs= c.getResult(query);
			while(rs.next()){
				file_name=rs.getString(2);
				batch_id=rs.getInt(1);
			}
			
			File f= new File("F:/Metalink/attachments/batch_"+ batch_id + "/TimeTable/" +file_name);
			boolean s=f.delete();
			System.out.println("deleted file is:"+s);
			c.dml(query2);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}

